function getParams() {
  var url = window.location.href
    .slice(window.location.href.indexOf("?") + 1)
    .split("&");
  var result = {};
  url.forEach(function(item) {
    var param = item.split("=");
    result[param[0]] = param[1];
  });
  return result;
}

function init() {
  Survey.dxSurveyService.serviceUrl = "";

  var css = {
    root: "sv_main sv_frame sv_default_css"
  };

  var surveyId = decodeURI(getParams()["id"]);
  var model = new Survey.Model({ surveyId: surveyId, surveyPostId: surveyId });
  model.css = css;
  window.survey = model;


  survey
    .onComplete
    .add(function (result) {
        document
            .querySelector('#surveyResult')
            .innerHTML = "result: " + JSON.stringify(result.data);
    });

// You can store file id as a content property of a file question value
// In this case you should handle both `onUploadFiles` and `onDownloadFile` events



survey
    .onDownloadFile
    .add((survey, options) => {
        var xhr = new XMLHttpRequest();
        xhr.responseType = "blob";
        xhr.open("GET", "/api/MySurveys/files?name=" + options.content); // "https://surveyjs.io/api/MySurveys/files?name=" + options.content
        xhr.onload = function () {
            var reader = new FileReader();
            reader.onload = function (e) {
                options.callback("success", e.target.result);
            };
            reader.readAsDataURL(new File([xhr.response], options.fileValue.name, {type: options.fileValue.type}));
        };
        xhr.send();
        // You can use fetch for modern browsers
        //fetch("/api/MySurveys/files?name=" + options.content)
        //  .then(function(response) {
        //    return response.blob();
        //  })
        //  .then(function(blob) {
        //    var reader = new FileReader();
        //    reader.onload = function(e) {
        //        options.callback("success", e.target.result);
        //    };
        //    reader.readAsDataURL(new File([blob], options.fileValue.name, { type: options.fileValue.type }));
        //    options.callback("success", URL.createObjectURL(blob));
        //  });
    });

// You can store file download url as a content property of a file question value
// In this case you can handle the `onUploadFiles` event only
// This example uses jQuery aiax function
/*
survey.onUploadFiles.add((survey, options) => {
    var formData = new FormData();
    options.files.forEach(function(file) {
        formData.append(file.name, file);
    });
    $.ajax({
        url: "/api/MySurveys/uploadFiles?accessKey=<your_access_key>", // https://surveyjs.io/api/MySurveys/uploadFiles
        type: "POST",
        xhr: function () {
            var myXhr = $.ajaxSettings.xhr();
            if (myXhr.upload) {
                myXhr.upload.addEventListener('progress', function (event) {
                    var percent = 0;
                    var position = event.loaded || event.position;
                    var total = event.total;
                }, false);
            }
            return myXhr;
        },
        success: function (data) {
            options.callback("success",
                options.files.map(file => {
                    return { file: file, content: "/api/MySurveys/files?name=" + data[file.name] };
                })
            );
        },
        error: function (error) {
        },
        async: true,
        data: formData,
        cache: false,
        contentType: false,
        processData: false,
        timeout: 60000
    });
});
*/


  model.render("surveyElement");

  // // Load survey by id from url
  // var xhr = new XMLHttpRequest();
  // xhr.open('GET', "https://surveyjs-php.herokuapp.com" + '/survey?surveyId=' + surveyId);
  // xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  // xhr.onload = function () {
  //     var result = JSON.parse(xhr.response);
  //     if(!!result) {
  //         var surveyModel = new Survey.Model(result);
  //         window.survey = surveyModel;
  //         ko.cleanNode(document.getElementById("surveyElement"));
  //         document.getElementById("surveyElement").innerText = "";
  //         surveyModel.render("surveyElement");
  //     }
  // };
  // xhr.send();
}

init();
